﻿using Microsoft.AspNetCore.Identity;
using ProgrammingEducationSystem.Models;
using ProgrammingEducationSystem.Repository.IRepository;
using ProgrammingEducationSystem.Services.IServices;

namespace ProgrammingEducationSystem.Services
{
    public class ClassesSerivce : IClassesService
    {
        private readonly IClassRepository _classRepository;
        private readonly IStuAndClassRepository _stuAndClassRepository;
        private readonly ICourseHourRepository _courseHourRepository;
        private readonly ICourseRepository _courseRepository;
        private readonly ILearnSourceRepository _learnSourceRepository;
        private readonly IEndClassRepository _endClassRepository;
        public ClassesSerivce(IClassRepository classRepository,IStuAndClassRepository stuAndClassRepository,
            ICourseHourRepository courseHourRepository,ICourseRepository courseRepository, 
            ILearnSourceRepository learnSourceRepository,IEndClassRepository endClassRepository)
        {
            _classRepository = classRepository;
            _stuAndClassRepository = stuAndClassRepository;
            _courseHourRepository = courseHourRepository;
            _courseRepository = courseRepository;
            _learnSourceRepository = learnSourceRepository;
            _endClassRepository = endClassRepository;
        }


        public async Task AddClassAsync(Class @class)
        {
            await _classRepository.AddAsync(@class);
        }

        public async Task<List<Class>?> AllAsync()
        {
            return await _classRepository.FindAllAsync();
        }

        public async Task DeleteAsync(Guid Id)
        {
            // 查找要删除的班级
            var @class = await _classRepository.FindAsync(Id);
            if (@class == null)
            {
                // 如果没有找到对应的班级，则中止删除操作
                return;
            }

            // 删除与班级相关联的用户
            await _stuAndClassRepository.DeleteByClassIdAsync(Id);
           

            // 删除与班级相关联的课时
            var courseHours = await _courseHourRepository.GetByClassIdAsync(Id);
            foreach (var courseHour in courseHours)
            {
                 _courseHourRepository.Delete(courseHour);
            }

            // 删除与班级相关联的课程
            var courses = await _courseRepository.GetByClassIdAsync(Id);
            foreach (var course in courses)
            {
                _courseRepository.Delete(course);
            }

            //由于外键修改，不在需要此逻辑
            //// 删除与班级相关联的学习资源
            //var learnSources = await _learnSourceRepository.GetByClassIdAsync(Id);
            //foreach (var learnSource in learnSources)
            //{
            //    _learnSourceRepository.Delete(learnSource);
            //}

            // 最后删除班级本身
            _classRepository.Delete(@class);
        }

        public async Task DeleteByTeacherIdAsync(string id)
        {
            await _classRepository.DeleteByTeacherIdAsync(id);
        }

        public async Task<List<Class>?> GetByEndClass()
        {
            var endInfos = await  _endClassRepository.FindAllAsync();
            var classes = await _classRepository.FindAllAsync();
            var result = classes.Where(a => endInfos.Any(b => b.ClassId == a.Id)).ToList();
            return result;
        }

        public async Task<List<Class>?> GetByEndClassAndStuId(string stuId)
        {
            var stuid = await _stuAndClassRepository.GetByStudentIdAsync(stuId);
            var endInfos = await _endClassRepository.FindAllAsync();

             var res = endInfos.Where(e => stuid.Any(s => s.ClassId == e.ClassId)).ToList();

            var result = await _classRepository.FindByIds(res.Select(r=>r.ClassId).ToList());

            
            return result;
        }

        public async Task<List<Class>?> GetByEndClassAndTeaId(string teaId)
        {
            var teaid = await _classRepository.GetByTeacherId(teaId);
            var endInfos = await _endClassRepository.FindAllAsync();
            var res = endInfos.Where(e => teaid.Any(t => t.Id == e.ClassId)).ToList();
            var result = await _classRepository.FindByIds(res.Select(r => r.ClassId).ToList());
            return result;
        }

        public async Task<Class?> GetById(Guid id)
        {
            return await _classRepository.FindAsync(id);
        }

        public async Task<List<Class>?> GetByStudentIdAsync(string id)
        {
          return await _classRepository.GetByStudentIdAsync(id);
        }

        public async Task<List<Class>?> GetByTeacherIdAsync(string id)
        {
            return await _classRepository.GetByTeacherId(id);
        }

        public async Task<bool> SaveAsync()
        {
            return await _classRepository.SaveAsync();
        }

        public void UpdateAsync(Class @class)
        {
            _classRepository.Update(@class);
        }

        
    }
}
